<template>
  <div class="form-item-el">
    <div v-if="mode === 'DESIGN'">
      <a-input disabled :placeholder="placeholder" />
    </div>
    <div v-else>
      <a-input
        v-if="!$attrs.disabled"
        ref="input"
        v-model="_value"
        :placeholder="placeholder"
        :disabled="$attrs.disabled"
        :allow-clear="$attrs.clearable"
      ></a-input>
      <template v-else>
        <a-tooltip :content="_value" position="top" v-if="isOver">
          <a-input ref="input" v-model="_value" disabled></a-input>
        </a-tooltip>
        <a-input v-else ref="input" v-model="_value" disabled></a-input>
      </template>
    </div>
  </div>
</template>

<script setup>
import { computed, getCurrentInstance, onMounted, ref, nextTick } from "vue";
import { isEmpty } from "lodash";

const { proxy } = getCurrentInstance();
const props = defineProps({
  mode: {
    type: String,
    default: "DESIGN"
  },
  required: {
    type: Boolean,
    default: false
  },
  readonly: {
    type: Boolean,
    default: false
  },
  modelValue: {
    type: String,
    default: ""
  },
  placeholder: {
    type: String,
    default: "请输入内容"
  }
});
const emit = defineEmits(["update:modelValue"]);

const _value = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emit("update:modelValue", val);
  }
});

const isOver = ref(false);
const checkOverflow = async () => {
  await nextTick();
  const inputElement = proxy.$refs.input.$el.querySelector("input");
  isOver.value = inputElement.scrollWidth > inputElement.clientWidth;
};

if (isEmpty(_value.value) && !proxy.$attrs.disabled) {
  _value.value = proxy.$attrs.defaultValue;
}

onMounted(() => {
  checkOverflow();
});
</script>

<style lang="scss" scoped>
.text-input {
  width: 100%;
}
</style>
